package c.c.b.a.b.a.h;

import c.c.b.a.a.e;
import c.c.b.a.a.f;
import c.c.b.a.b.a.h.d.b;
import com.pgl.sys.ces.out.ISdkLite;
import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

/* compiled from: Http2Writer */
public final class r implements Closeable {
    public static final Logger g = Logger.getLogger(e.class.getName());
    public final f a;
    public final boolean b;
    public final e c;
    public int d = 16384;
    public boolean e;
    public final b f;

    public r(f fVar, boolean z) {
        this.a = fVar;
        this.b = z;
        e eVar = new e();
        this.c = eVar;
        this.f = new b(eVar);
    }

    public void a(int i, int i2, byte b, byte b2) throws IOException {
        Logger logger = g;
        if (logger.isLoggable(Level.FINE)) {
            logger.fine(e.b(false, i, i2, b, b2));
        }
        if (i2 > this.d) {
            e.a("FRAME_SIZE_ERROR length > %d: %d", Integer.valueOf(this.d), Integer.valueOf(i2));
            throw null;
        } else if ((Integer.MIN_VALUE & i) == 0) {
            f fVar = this.a;
            fVar.h((i2 >>> 16) & ISdkLite.REGION_UNSET);
            fVar.h((i2 >>> 8) & ISdkLite.REGION_UNSET);
            fVar.h(i2 & ISdkLite.REGION_UNSET);
            this.a.h(b & ISdkLite.REGION_UNSET);
            this.a.h(b2 & ISdkLite.REGION_UNSET);
            this.a.e(i & Integer.MAX_VALUE);
        } else {
            e.a("reserved bit set: %s", Integer.valueOf(i));
            throw null;
        }
    }

    public synchronized void close() throws IOException {
        this.e = true;
        this.a.close();
    }

    public synchronized void k(int i, long j) throws IOException {
        if (this.e) {
            throw new IOException("closed");
        } else if (j == 0 || j > 2147483647L) {
            e.a("windowSizeIncrement == 0 || windowSizeIncrement > 0x7fffffffL: %s", Long.valueOf(j));
            throw null;
        } else {
            a(i, 4, (byte) 8, (byte) 0);
            this.a.e((int) j);
            this.a.flush();
        }
    }

    public synchronized void n(int i, b bVar) throws IOException {
        if (this.e) {
            throw new IOException("closed");
        } else if (bVar.a != -1) {
            a(i, 4, (byte) 3, (byte) 0);
            this.a.e(bVar.a);
            this.a.flush();
        } else {
            throw new IllegalArgumentException();
        }
    }

    public synchronized void o(int i, b bVar, byte[] bArr) throws IOException {
        if (this.e) {
            throw new IOException("closed");
        } else if (bVar.a != -1) {
            a(0, bArr.length + 8, (byte) 7, (byte) 0);
            this.a.e(i);
            this.a.e(bVar.a);
            if (bArr.length > 0) {
                this.a.y(bArr);
            }
            this.a.flush();
        } else {
            e.a("errorCode.httpCode == -1", new Object[0]);
            throw null;
        }
    }

    public synchronized void q(v vVar) throws IOException {
        if (this.e) {
            throw new IOException("closed");
        }
        int i = this.d;
        int i2 = vVar.a;
        if ((i2 & 32) != 0) {
            i = vVar.b[5];
        }
        this.d = i;
        i = i2 & 2;
        int i3 = -1;
        if ((i != 0 ? vVar.b[1] : -1) != -1) {
            b bVar = this.f;
            if (i != 0) {
                i3 = vVar.b[1];
            }
            Objects.requireNonNull(bVar);
            int min = Math.min(i3, 16384);
            i = bVar.d;
            if (i != min) {
                if (min < i) {
                    bVar.b = Math.min(bVar.b, min);
                }
                bVar.c = true;
                bVar.d = min;
                i = bVar.h;
                if (min < i) {
                    if (min == 0) {
                        bVar.a();
                    } else {
                        bVar.f(i - min);
                    }
                }
            }
        }
        a(0, 0, (byte) 4, (byte) 1);
        this.a.flush();
    }

    public synchronized void r(boolean z, int i, int i2) throws IOException {
        if (this.e) {
            throw new IOException("closed");
        }
        a(0, 8, (byte) 6, z ? (byte) 1 : (byte) 0);
        this.a.e(i);
        this.a.e(i2);
        this.a.flush();
    }

    public synchronized void s(boolean z, int i, e eVar, int i2) throws IOException {
        if (this.e) {
            throw new IOException("closed");
        }
        a(i, i2, (byte) 0, z ? (byte) 1 : (byte) 0);
        if (i2 > 0) {
            this.a.g0(eVar, (long) i2);
        }
    }

    public void t(boolean z, int i, List<c> list) throws IOException {
        if (this.e) {
            throw new IOException("closed");
        }
        this.f.e(list);
        long j = this.c.b;
        int min = (int) Math.min((long) this.d, j);
        long j2 = (long) min;
        int i2 = (j > j2 ? 1 : (j == j2 ? 0 : -1));
        byte b = i2 == 0 ? (byte) 4 : (byte) 0;
        if (z) {
            b = (byte) (b | 1);
        }
        a(i, min, (byte) 1, b);
        this.a.g0(this.c, j2);
        if (i2 > 0) {
            w(i, j - j2);
        }
    }

    public synchronized void v() throws IOException {
        if (this.e) {
            throw new IOException("closed");
        }
        this.a.flush();
    }

    public final void w(int i, long j) throws IOException {
        while (j > 0) {
            int min = (int) Math.min((long) this.d, j);
            long j2 = (long) min;
            j -= j2;
            a(i, min, (byte) 9, j == 0 ? (byte) 4 : (byte) 0);
            this.a.g0(this.c, j2);
        }
    }
}
